package ph.com.gs3.formalistics.fragments.dialogs;

import java.util.List;

import ph.com.gs3.formalistics.interfaces.CallbackCommand;
import ph.com.gs3.formalistics.model.valueobjects.application.FormStub;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

public class FormSelectionDialogFragment extends DialogFragment {

	public static String TAG = FormSelectionDialogFragment.class.getSimpleName();

	public static enum FormsAvailability {
		CURRENTLY_UPDATING, NO_FORMS_TO_SHOW, HAS_FORMS
	}

	private List<FormStub> formSelectionStubs;
	private FormsAvailability formsAvailability;

	private CallbackCommand<FormStub> formSelectedCallback;

	public FormSelectionDialogFragment() {
		formsAvailability = FormsAvailability.NO_FORMS_TO_SHOW;
	}

	public void setFormStubs(List<FormStub> formStubs) {
		formSelectionStubs = formStubs;
	}

	public void setFormsAvailability(FormsAvailability formsAvailability) {
		this.formsAvailability = formsAvailability;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		switch (formsAvailability) {
			case HAS_FORMS:
				return createFormListDialog();
			case NO_FORMS_TO_SHOW:
				return createNoAvailableFormsDialog();
			case CURRENTLY_UPDATING:
				return createFormListUpdatingDialog();
			default:
				return null;
		}
	}

	private AlertDialog createNoAvailableFormsDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		builder.setTitle("Sorry").setMessage("No available forms yet.")
		        .setNegativeButton("OK", null);

		return builder.create();

	}

	private AlertDialog createFormListDialog() {

		AlertDialog.Builder builder = createFormListDialogBuilder();

		return builder.create();

	}

	private AlertDialog createFormListUpdatingDialog() {

		AlertDialog.Builder builder;

		if (formSelectionStubs.size() >= 1) {
			builder = createFormListDialogBuilder();
		} else {
			builder = new AlertDialog.Builder(getActivity());
			builder.setTitle("Please wait...").setMessage("Forms are updating...")
			        .setNegativeButton("OK", null);
		}

		return builder.create();

	}

	private AlertDialog.Builder createFormListDialogBuilder() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		String[] formNames = new String[formSelectionStubs.size()];

		for (int i = 0; i < formSelectionStubs.size(); i++) {
			formNames[i] = formSelectionStubs.get(i).name;
		}

		builder.setTitle("Create using which form?").setItems(formNames,
		        new DialogInterface.OnClickListener() {

			        @Override
			        public void onClick(DialogInterface dialog, int which) {

				        if (formSelectedCallback != null) {
					        formSelectedCallback.execute(formSelectionStubs.get(which));
				        }

			        }
		        });
		return builder;

	}

	public void setOnFormSelectedCallback(CallbackCommand<FormStub> formSelectedCallback) {
		this.formSelectedCallback = formSelectedCallback;
	}

}
